package com.api.syh56.shengyunhui.controller;


import com.api.syh56.shengyunhui.common.BaseUserInfo;
import com.api.syh56.shengyunhui.common.CommonPage;
import com.api.syh56.shengyunhui.common.CommonResult;
import com.api.syh56.shengyunhui.common.UserInfoEnu;
import com.api.syh56.shengyunhui.entity.Address;
import com.api.syh56.shengyunhui.entity.SystemMessage;
import com.api.syh56.shengyunhui.service.AddressService;
import com.api.syh56.shengyunhui.service.SystemMessageService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 用户消息
 */
@RestController
@RequestMapping("systemMessage")
@Slf4j

public class SystemMessageController {
    @Autowired
    private SystemMessageService systemMessageService;

    /**
     * 阅读消息
     * @param id
     * @return
     */
    @GetMapping("read")
    public CommonResult<SystemMessage> addNewMessage(@RequestParam Long id){
//        address.setUid();
        SystemMessage systemMessage = systemMessageService.readMessage(id);
        return CommonResult.success(systemMessage);
    }

    /**
     * 一键已读
     */
    @PostMapping("Allread")
    public CommonResult<String> Allread(){
        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        systemMessageService.readAllMessage(uid);
        return CommonResult.success("一键已读成功");
    }


    @PostMapping("delete")
    public CommonResult<Object> deleteMessage(@RequestBody SystemMessage systemMessage){
        System.out.println(systemMessage.getId());
        systemMessageService.deleteMessage(systemMessage.getId());
        return CommonResult.success(null);
    }


    /**
     * 未读消息数量
     */
    @GetMapping("getUnReadMessageNum")
    public CommonResult<Integer> getUnReadMessageNum(){
        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        int num = systemMessageService.getReceivedMessageNotRead(uid, 1, 100).getRecords().size();
        if (num > 100){
            num = 100;
        }
        return CommonResult.success(num);
    }


    /**
     * 获取所有消息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("getAllMessage")
    public CommonResult<CommonPage<SystemMessage>> getAllMessage(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        Page<SystemMessage> receivedMessageNotRead = systemMessageService.getAllMessage(uid, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(receivedMessageNotRead));
    }

    @GetMapping("getReceivedMessageNotRead")
    public CommonResult<CommonPage<SystemMessage>> getReceivedMessageNotRead(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        Page<SystemMessage> receivedMessageNotRead = systemMessageService.getReceivedMessageisRead(uid, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(receivedMessageNotRead));
    }

    @GetMapping("getReceivedMessageisRead")
    public CommonResult<CommonPage<SystemMessage>> getReceivedMessageisRead(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        Long uid = Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode()));
        Page<SystemMessage> receivedMessageisRead = systemMessageService.getReceivedMessageisRead(uid, pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(receivedMessageisRead));
    }

    @PostMapping("remove")
    public CommonResult<Object> remove(){
        systemMessageService.removeN();
        return CommonResult.success(null);
    }

}
